import mongoose from './mongoose';
import redis from './redis';
import socket from './socket';
import apiBridge from './apiBridge';

class ComponentContainer {
  constructor() {
    this.cache = {};
  }

  getComponent(key, factory) {
    if (this.cache[key]) return this.cache[key];

    const result = factory(this.app, this);
    this.cache[key] = result;
    return result;
  }

  getMongooseClient() {
    return this.getComponent('mongoose', mongoose);
  }

  getRedis() {
    return this.getComponent('redis', redis);
  }

  getSocket() {
    return this.getComponent('sock', socket);
  }

  getApiBridge() {
    return this.getComponent('apiBridge', apiBridge);
  }

  init(app) {
    this.app = app;

    // preload components
    this.getMongooseClient();
    this.getRedis();
    this.getSocket();
    this.getApiBridge();
  }
}

export default new ComponentContainer();
